package com.jowell.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
//import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.jowell.mall.product.entity.CategoryEntity;
import com.jowell.mall.product.service.CategoryService;
import com.jowell.common.utils.PageUtils;
import com.jowell.common.utils.Result;

/**
 * 商品三级分类
 *
 * @author Mr.ZJW
 * @email zjw@gmail.com
 * @date 2022-06-10 10:02:03
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:category:list")
    public Result list(@RequestParam Map<String, Object> params) {
        PageUtils page = categoryService.queryPage(params);
        return Result.ok().put("page", page);
    }

    /**
     * @param [params]
     * @Author: Mr.ZJW
     * @Description: 查询所有类别数据，然后将数据封装为树形结构
     * @Date: 2022/6/15 13:53
     **/
    @GetMapping("/listTree")
    //@RequiresPermissions("product:category:list")
    public Result listTree(@RequestParam Map<String, Object> params) {
        List<CategoryEntity> list = categoryService.listTree(params);
        return Result.ok().put("data", list);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    //@RequiresPermissions("product:category:info")
    public Result info(@PathVariable("catId") Long catId) {
        CategoryEntity category = categoryService.getById(catId);

        return Result.ok().put("category", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:category:save")
    public Result save(@RequestBody CategoryEntity category) {
        categoryService.save(category);

        return Result.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:category:update")
    public Result update(@RequestBody CategoryEntity category) {
        categoryService.updateById(category);

        return Result.ok();
    }

    /**
     * @Author: Mr.ZJW
     * @Description: 批量修改
     * @param category
     * @Date: 2022/6/28 16:23
    **/
    @RequestMapping("/BatchUpdate")
    //@RequiresPermissions("product:category:update")
    public Result BatchUpdate(@RequestBody List<CategoryEntity> category) {
        return Result.ok(categoryService.BatchUpdate(category));
    }

    /**
     * 逻辑删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:category:delete")
    public Result delete(@RequestBody Long[] catIds) {
        categoryService.removeCategoryByIds(Arrays.asList(catIds));
        return Result.ok();
    }

}
